package com.xyz.rok.tools.controller;

import com.xyz.rok.tools.constant.SystemCode;
import com.xyz.rok.tools.model.Command;
import com.xyz.rok.tools.model.Result;
import com.xyz.rok.tools.service.CommandService;
import com.xyz.rok.tools.service.ConfigService;
import com.xyz.rok.tools.utils.OCRUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * @author ZhangYiXin
 * @version 1.0
 * @date 2021/4/14 17:29
 **/
@Slf4j
@RestController
@RequestMapping("/api/ocr")
public class OCRController {

    private final int[] whiteRgbRange = {110, 255, 150, 255, 150, 255};
    private final int[] blackRgbRange = {0, 155, 0, 155, 0, 155};
    private final int[] blueRgbRange = {60, 99, 160, 195, 200, 222};
    @Value("${upload.path:/app/upload/}")
    private String uploadPath;
    @Value("${ocr.error-path:/app/upload/error/}")
    private String ocrErrorPath;
    @Value("${traineddata.path:/app/traineddata/}")
    private String dataPath;
    @Autowired
    private ConfigService configModelService;
    @Autowired
    private CommandService commandService;

    private ConcurrentLinkedQueue<Command> commandQueue = new ConcurrentLinkedQueue();


    /**
     * R1,R2,R3,R4人数
     *
     * @param file
     * @return
     */
    @PostMapping("/unionMember")
    public Result getUnionMemberCount(@RequestParam("file") MultipartFile file) {
        String fileName = "unionMember_" + file.getOriginalFilename();
        String filePath = uploadPath + fileName;
        File dest = new File(filePath);
        try {
            file.transferTo(dest);
            List<Integer> memberCount = OCRUtil.getUnionMemberCount(OCRUtil.ocrImage(whiteRgbRange, filePath, dataPath));
            dest.delete();
            return Result.success(memberCount);
        } catch (IOException e) {
            log.error("文件上传失败", e);
        } catch (Exception e) {
            log.error("图片解析失败", e);
            dest.renameTo(new File(ocrErrorPath + fileName));
        }
        return Result.error(SystemCode.SYS_UPLOAD_FAILED);
    }

    @PostMapping("/gameId")
    public Result getGameID(@RequestParam("file") MultipartFile file) {
        String fileName = "gameId_" + file.getOriginalFilename();
        String filePath = uploadPath + fileName;
        File dest = new File(filePath);
        try {
            file.transferTo(dest);
            Integer gameId = OCRUtil.getId(OCRUtil.ocrImage(blueRgbRange, filePath, dataPath));
            dest.delete();
            return Result.success(gameId);
        } catch (IOException e) {
            log.error("文件上传失败", e);
        } catch (Exception e) {
            log.error("图片解析失败", e);
            dest.renameTo(new File(ocrErrorPath + fileName));
        }
        return Result.error(SystemCode.SYS_UPLOAD_FAILED);
    }


    @PostMapping("/currentPower")
    public Result getCurrentPower(@RequestParam("file") MultipartFile file) {
        String fileName = "currentPower_" + file.getOriginalFilename();
        String filePath = uploadPath + fileName;
        File dest = new File(filePath);
        try {
            file.transferTo(dest);
            Integer power = OCRUtil.getPower(OCRUtil.ocrImage(whiteRgbRange, filePath, dataPath));
            dest.delete();
            return Result.success(power);
        } catch (IOException e) {
            log.error("文件上传失败", e);
        } catch (Exception e) {
            log.error("图片解析失败", e);
            dest.renameTo(new File(ocrErrorPath + fileName));
        }
        return Result.error(SystemCode.SYS_UPLOAD_FAILED);
    }

    @PostMapping("/death")
    public Result getDeath(@RequestParam("file") MultipartFile file) {
        String fileName = "death_" + file.getOriginalFilename();
        String filePath = uploadPath + fileName;
        File dest = new File(filePath);
        try {
            file.transferTo(dest);
            Integer[] death = OCRUtil.getDeath(OCRUtil.ocrImage(whiteRgbRange, filePath, dataPath));
            dest.delete();
            return Result.success(death);
        } catch (IOException e) {
            log.error("文件上传失败", e);
        } catch (Exception e) {
            log.error("图片解析失败", e);
            dest.renameTo(new File(ocrErrorPath + fileName));
        }
        return Result.error(SystemCode.SYS_UPLOAD_FAILED);
    }

    @PostMapping("/killCount")
    public Result getKillCount(@RequestParam("file") MultipartFile file) {
        String fileName = "killCount_" + file.getOriginalFilename();
        String filePath = uploadPath + fileName;
        File dest = new File(filePath);
        try {
            file.transferTo(dest);
            List<Integer> killCount = OCRUtil.getKillCount(OCRUtil.ocrImage(blackRgbRange, filePath, dataPath));
            dest.delete();
            return Result.success(killCount);
        } catch (IOException e) {
            log.error("文件上传失败", e);
        } catch (Exception e) {
            log.error("图片解析失败", e);
            dest.renameTo(new File(ocrErrorPath + fileName));
        }
        return Result.error(SystemCode.SYS_UPLOAD_FAILED);
    }

}
